package com.wanmait.myhouse.managerController;
import com.wanmait.myhouse.dao.ManagerDAO;
import com.wanmait.myhouse.dao.MediatorDAO;
import com.wanmait.myhouse.vo.Mediator;
import com.wanmait.myhouse.vo.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("manager")
public class ManagerDesignController {

    @GetMapping("mediatorPage")
    public String mediatorPage(Integer pno , Model model){
        MediatorDAO mediatorDAO = new MediatorDAO();
        Pager pager = new Pager();
        if (pno == null){
            pno = 1;
        }
        pager.setPageNO(pno);
        pager.setPageSize(4);
        int total = mediatorDAO.getTotal();
        int pageCount = total/pager.getPageSize()+(total%pager.getPageSize()==0?0:1);
        pager.setPageCount(pageCount);
        model.addAttribute("pager",pager);
        List<Mediator> mediatorList = mediatorDAO.findByPageno(pager);
        model.addAttribute("mediatorList",mediatorList);
        return "/manager/design";
    }

    @GetMapping("banMediator")
    public String banMediator(Integer id,Boolean ban,HttpServletRequest request){
        String uri = request.getHeader("referer");
        if (ban == true){
            ban = false;
        }else {
            ban = true;
        }
        MediatorDAO mediatorDAO = new MediatorDAO();
        mediatorDAO.banMediator(id,ban);
        return "redirect:"+uri;
    }


    @GetMapping("design")
    public String design(){
        return "/manager/design";
    }
}
